package com.core.service.Admin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.mapper.ReservationMapper;
import com.core.pojo.Result.PageResult;
import com.core.pojo.Result.Result;
import com.core.pojo.SeatReservation.entity.Reservation;
import com.core.pojo.User.entity.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class AdminServiceImpl implements AdminService {
    private final ReservationMapper reservationMapper;

    @Override
    public Result<PageResult<Reservation>> queryAllReservations(int page, int pageSize) {
        // 1. 启动 PageHelper 分页
        PageHelper.startPage(page, pageSize);

        // 2. 执行分页查询
        List<Reservation> reservationList = reservationMapper.selectList(null);

        // 3. 使用 PageInfo 包装查询结果，以获取详细的分页信息
        PageInfo<Reservation> pageInfo = new PageInfo<>(reservationList);

        // 4. 转换并返回自定义分页结构
        return Result.success(PageResult.fromPageInfo(pageInfo));
    }

    @Override
    public Result<PageResult<Reservation>> queryUserReservations(int page, int pageSize, Long userId) {
        // 1. 启动 PageHelper 分页
        PageHelper.startPage(page, pageSize);

        // 2. 执行查询
        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Reservation::getUserId, userId); // 假设 Reservation 实体有 userId 字段
        queryWrapper.orderByAsc(Reservation::getCreateTime); // 按创建时间降序排序
        List<Reservation> reservationList = reservationMapper.selectList(queryWrapper);

        // 3. 使用 PageInfo 包装查询结果，以获取详细的分页信息
        PageInfo<Reservation> pageInfo = new PageInfo<>(reservationList);

        // 4. 封装成自定义的 PageResult 对象
        PageResult<Reservation> pageResult = PageResult.fromPageInfo(pageInfo);

        // 5. 返回成功结果
        return Result.success(pageResult);
    }
}
